/* 
 * File:   main.c
 * Author: Jung
 *
 * Created on 30 de Março de 2012, 15:42
 */
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>

#include <time.h>

#define NUM_THREADS 10
#define NUM_REQUISICOES 100

int contaThead = -1;
char *ips[5];

char mensagem[20];

typedef struct {
    char id[8];
    int etapa;
    int tempo;
} requisicao;

void teste() {
    
    ips[0] = malloc(15 * sizeof(char)); //Para 30 caracteres no máximo
    strcpy(ips[0], "127.0.0.1");
     ips[1] = malloc(15 * sizeof(char)); //Para 30 caracteres no máximo
    strcpy(ips[1], "127.0.0.2");
     ips[2] = malloc(15 * sizeof(char)); //Para 30 caracteres no máximo
    strcpy(ips[2], "127.0.0.3");
     ips[3] = malloc(15 * sizeof(char)); //Para 30 caracteres no máximo
    strcpy(ips[3], "127.0.0.4");
     ips[4] = malloc(15 * sizeof(char)); //Para 30 caracteres no máximo
    strcpy(ips[4], "127.0.0.5");
    
}




void *PrintHello(void *threadid) {

    mensagem[0] = '0';
    mensagem[1] = '0';
    mensagem[2] = '0';
    mensagem[3] = '0';
    mensagem[4] = '0';
    mensagem[5] = '0';
    mensagem[6] = '0';
    mensagem[7] = '0';
    mensagem[8] = '#';
   
    mensagem[9] = '0';
    mensagem[10] = '#';
    
    mensagem[11] = '0';
    mensagem[12] = '#';
    
   
    char requisicoes[NUM_REQUISICOES][20];
    
    char car[10];
    car[0] = '0';car[1] = '1';car[2] = '2';car[3] = '3';car[4] = '4';car[5] = '5';car[6] = '6';car[7] = '7';car[8] = '8';car[9] = '9';

    int c = 0;
    for(c = 0; c < NUM_REQUISICOES-1; c++) {
        requisicoes[c][0] = car[random()%10];
        requisicoes[c][1] = car[random()%10];
        requisicoes[c][2] = car[random()%10];
        requisicoes[c][3] = car[random()%10];
        requisicoes[c][4] = car[random()%10];
        requisicoes[c][5] = car[random()%10];
        requisicoes[c][6] = car[random()%10];
        requisicoes[c][7] = car[random()%10];
        requisicoes[c][8] = '#';
        
        requisicoes[c][9] = '0';
        requisicoes[c][10] = '#';
        
        requisicoes[c][11] = '0';
        requisicoes[c][12] = '#';
        //printf("%s\n",requisicoes[c]);
    }
    
   //if(contaThead<3) contaThead++;
    //else contaThead = 0;
    contaThead = random()%4;
    //printf(" vvvv %d\n",contaThead);
    
    int sockfd;
    int len;
    struct sockaddr_in address; 
    int result;
    char *ch;
    
    for(c = 0 ; c < 3; c++) {
        sockfd = socket(AF_INET, SOCK_STREAM, 0);
        address.sin_family = AF_INET;
        
        contaThead = random()%5;
        address.sin_addr.s_addr= inet_addr(ips[contaThead]);

        //printf("ccc  %s\n", ips[contaThead]);

        address.sin_port= 9734;
        len= sizeof(address)*2; 
        result = connect(sockfd, (struct sockaddr*)&address, len);
        while(result == -1) {
            
            perror("oops: client1");
            printf(">>> %d\n", contaThead );
                //exit(1);
            
            contaThead = random()%5;
                address.sin_addr.s_addr= inet_addr(ips[contaThead]);
                len= sizeof(address)*2; 
            //sleep(5);
            result = connect(sockfd, (struct sockaddr*)&address, len);
        }


        write(sockfd, &requisicoes[c], sizeof(char)*30);
        printf("enviando  %s para servidor %d \n", requisicoes[c],sockfd);
        read(sockfd, &mensagem, sizeof(char)*30);
        printf("recebendo %s do servidor %d \n", mensagem, sockfd);
        close(sockfd);
    }
    
    
    //exit(0);
    
    
}

int main(int argc, char*argv[]) {
    
    
    
    teste();
    
    
    pthread_t threads[NUM_THREADS];
    int rc;
    long t;
    for(t=0; t<NUM_THREADS; t++){
        //printf("In main: creating thread %ld\n", t);
        
        //printf("%d\n",t);
        //contaThead = 0;
        //contaThead = rand() % 5;
        rc= pthread_create(&threads[t], NULL, PrintHello, (void*)t);
        if(rc){
            printf("ERROR; return code from pthread_create() is %d\n", rc);
            exit(-1);
        }
    }
    
    system("read x");
    pthread_exit(NULL);
    
    
    
    
    
    return (EXIT_SUCCESS);
}

